//
//  MBProgressHUD+Progress.m
//  MeiYai
//
//  Created by ArthurWang on 16/2/2.
//  Copyright © 2016年 MeiYai. All rights reserved.
//

#import "MBProgressHUD+Progress.h"

@implementation MBProgressHUD (Progress)

+ (MB_INSTANCETYPE)showDrawInViewWithoutIndicator:(UIView *)view status:(NSString *)text afterDelay:(NSTimeInterval)delay
{
    MBProgressHUD * hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
    [hud setMode:MBProgressHUDModeCustomView];
    hud.customView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"ic_done"]];
    hud.detailsLabelText = text;
    [hud hide:YES afterDelay:delay];
    
    return hud;
}

+ (MB_INSTANCETYPE)showInViewWithoutIndicator:(UIView *)view status:(NSString *)text afterDelay:(NSTimeInterval)delay
{
    MBProgressHUD * hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
    [hud setMode:MBProgressHUDModeText];
    hud.detailsLabelText = text;
    [hud hide:YES afterDelay:delay];
    
    return hud;
}

+ (MB_INSTANCETYPE)showInView:(UIView *)view
{
    MBProgressHUD * hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
    
    return hud;
}

+ (MB_INSTANCETYPE)showInView:(UIView *)view status:(NSString *)text
{
    MBProgressHUD * hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
    hud.detailsLabelText = text;
    return hud;
}

#pragma mark - 提示框
MBProgressHUD *_HUD = nil;

+ (void)showProgressMsg:(NSString *)msg dim:(BOOL)dim
{
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    
    if (_HUD == nil) {
        _HUD = [[MBProgressHUD alloc] initWithWindow:window];
        _HUD.margin = 15;
        _HUD.cornerRadius = 4;
        [window addSubview:_HUD];
    }
    
    if (_HUD.superview == nil) {
        [window addSubview:_HUD];
    }
    
    [window bringSubviewToFront:_HUD];
    
    _HUD.yOffset = 0;
    _HUD.mode = MBProgressHUDModeIndeterminate;
    _HUD.dimBackground = NO;
    _HUD.animationType = MBProgressHUDAnimationZoom;
    _HUD.userInteractionEnabled = YES;
    //    _HUD.color = [UIColor colorWithHex:0xEFEFF4 alpha:0.8];
    _HUD.labelColor = [UIColor whiteColor];
    _HUD.labelText = msg;
    
    [_HUD show:YES];
}

+ (void)hideProgressMsg
{
    if (_HUD != nil) {
        [_HUD hide:YES];
    }
    _HUD = nil;
}

+ (void)hideProgressMsgDelay:(NSTimeInterval)delay
{
    if (_HUD != nil && ![_HUD isHidden]) {
        [_HUD hide:YES afterDelay:delay];
    }
    _HUD = nil;
}

+ (void)showInfoMsg:(NSString *)msg
{
    
    if (msg == nil || msg.length < 1) {
        return;
    }
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    if (_HUD == nil) {
        _HUD = [[MBProgressHUD alloc] initWithWindow:window];
        _HUD.margin = 15;
        _HUD.cornerRadius = 4;
        _HUD.mode = MBProgressHUDModeDeterminate;
        [window addSubview:_HUD];
    }
    
    if (_HUD.superview == nil) {
        [window addSubview:_HUD];
    }
    
    [window bringSubviewToFront:_HUD];
    
    _HUD.mode = MBProgressHUDModeText;
    _HUD.dimBackground = NO;
    _HUD.animationType = MBProgressHUDAnimationFade;
    _HUD.userInteractionEnabled = YES;
//    _HUD.color = [UIColor colorWithHex:0xE31747 alpha:0.8];
    _HUD.labelColor = [UIColor whiteColor];
//    _HUD.labelText = msg;
    _HUD.detailsLabelText = msg;
    _HUD.detailsLabelFont = [UIFont boldSystemFontOfSize:16];
    
    [_HUD show:YES];
    [_HUD hide:YES afterDelay:1.5];
    
    UITapGestureRecognizer *gesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideProgressMsg)];
    [_HUD addGestureRecognizer:gesture];
    
    UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(hideProgressMsg)];
    [swipe setDirection:UISwipeGestureRecognizerDirectionUp | UISwipeGestureRecognizerDirectionDown];
    [_HUD addGestureRecognizer:swipe];
}

+ (void)showErrorMsg:(NSString *)errMsg
{
    
    if (errMsg == nil || errMsg.length < 1) {
        return;
    }
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    if (_HUD == nil) {
        _HUD = [[MBProgressHUD alloc] initWithWindow:window];
        _HUD.margin = 15;
        _HUD.cornerRadius = 4;
        [window addSubview:_HUD];
    }
    
    if (_HUD.superview == nil) {
        [window addSubview:_HUD];
    }
    
    [window bringSubviewToFront:_HUD];
    
    _HUD.mode = MBProgressHUDModeText;
    _HUD.dimBackground = NO;
    _HUD.animationType = MBProgressHUDAnimationFade;
    _HUD.userInteractionEnabled = YES;
//    _HUD.color = [UIColor colorWithHex:0x0 alpha:0.8];
    _HUD.labelColor = [UIColor whiteColor];
//    _HUD.labelText = errMsg;
    _HUD.detailsLabelText = errMsg;
    _HUD.detailsLabelFont = [UIFont boldSystemFontOfSize:16];
    
    
    [_HUD show:YES];
    [_HUD hide:YES afterDelay:1.5];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideProgressMsg)];
    [_HUD addGestureRecognizer:tap];
    UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(hideProgressMsg)];
    [swipe setDirection:UISwipeGestureRecognizerDirectionUp | UISwipeGestureRecognizerDirectionDown];
    [_HUD addGestureRecognizer:swipe];
}

+ (void)showCustomHUDView:(NSString *)image msg:(NSString *)message
{
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    if (_HUD == nil) {
        _HUD = [[MBProgressHUD alloc] initWithWindow:window];
        _HUD.margin = 15;
        _HUD.cornerRadius = 4;
        [window addSubview:_HUD];
    }
    
    if (_HUD.superview == nil) {
        [window addSubview:_HUD];
    }
    
    [window bringSubviewToFront:_HUD];
    
    _HUD.customView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:image]];
    _HUD.mode = MBProgressHUDModeCustomView;
    _HUD.dimBackground = NO;
    _HUD.animationType = MBProgressHUDAnimationFade;
    _HUD.userInteractionEnabled = YES;
    //    _HUD.color = [UIColor colorWithHex:0x0 alpha:0.8];
    _HUD.labelColor = [UIColor whiteColor];
    _HUD.labelText = message;
    
    [_HUD show:YES];
    [_HUD hide:YES afterDelay:1];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideProgressMsg)];
    [_HUD addGestureRecognizer:tap];
    UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(hideProgressMsg)];
    [swipe setDirection:UISwipeGestureRecognizerDirectionUp | UISwipeGestureRecognizerDirectionDown];
    [_HUD addGestureRecognizer:swipe];
}


@end
